﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OffLineFilter
{
    public class IdfFilter
    {
        public const double MinIdf = 500;
        protected const double MaxIdf = 191703;
        protected double maxDefectsPercent = 0.5;
        protected Dictionary dictionary;

        public double Defects { get; set; }

        public IdfFilter(Dictionary dictionary, int maxDefectsPercent = 50)
        {
            this.dictionary = dictionary;
            this.maxDefectsPercent = (double)maxDefectsPercent / (double)100;
        }

        public bool MakeCheck(String[] words)
        {
            var missingWords = (from word in words
                               where dictionary[word] == null
                               select word).Distinct();

            if (missingWords.Count() > 0)
            {
                double maxIdf = dictionary.MaxIdf;
                foreach (String word in missingWords)
                    dictionary[word] = new Dictionary.NumericPair(1, maxIdf);
            }

            var badWords = from word in words
                           where dictionary[word].IdfValue < MinIdf || dictionary[word].IdfValue > MaxIdf
                           select word;

            Defects = (double)badWords.Count() / (double)words.Count();
            if (Defects > maxDefectsPercent)
                return false;

            return true;
        }
    }
}
